package xyz.hubery.event;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import xyz.hubery.domain.DepthItemVo;
import xyz.hubery.dto.MarketDto;
import xyz.hubery.enums.DepthMergeType;
import xyz.hubery.feign.MarketServiceFeign;
import xyz.hubery.feign.MatchServiceFeign;
import xyz.hubery.model.DepthsVo;
import xyz.hubery.model.MessagePayload;
import xyz.hubery.stream.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.MimeTypeUtils;
import cn.hutool.json.JSONObject;
import java.util.List;
import java.util.Map;

/**
 * 深度盘口数据事件
 */
@Component
@Slf4j
public class DepthEvent implements Event {

    @Autowired
    private MarketServiceFeign marketServiceFeign;
    @Autowired
    private MatchServiceFeign matchServiceFeign;

    /**
     * 盘口深度数据的组
     */
    private static final String DEPTH_GROUP = "market.%s.depth.step%s";


    @Autowired
    private Source source;

    /**
     * 推送市场合并深度
     */
    @Override
    public void handle() {
        // 获取所有的交易市场
        List<MarketDto> marketDtos = marketServiceFeign.findAllMarkets();
        for (MarketDto marketDto : marketDtos) {
            // 获取市场的深度数据
            String symbol = marketDto.getSymbol(); // 交易对
            // 获取该交易对下所有深度类型的数据
            for (DepthMergeType mergeType : DepthMergeType.values()) { // 遍历所有的深度类型
                int step = mergeType.getValue();

                // 获取盘口数据
                Map<String, List<DepthItemVo>> depthsMap = matchServiceFeign.querySymbolDepth(symbol);
                DepthsVo depthsVo = new DepthsVo();
                depthsVo.setPrice(marketDto.getOpenPrice());
                depthsVo.setCnyPrice(marketDto.getOpenPrice());
                depthsVo.setAsks(depthsMap.get("asks")); // 买单数据
                depthsVo.setBids(depthsMap.get("bids")); // 卖单数据
                String data = JSON.toJSONString(depthsVo);

                MessagePayload messagePayload = new MessagePayload();
                // 内容
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("tick", data);
                messagePayload.setBody(jsonObject.toString());
                // 频道
                String channel = String.format(DEPTH_GROUP, symbol.toLowerCase(), step);
                messagePayload.setChannel(channel);

                String s = jsonObject.toString();
                log.warn("DepthEvent 消息内容大小：{}",s.getBytes().length);

                // 发布事件
                source.subscribeEventOutput()
                        .send(
                                MessageBuilder
                                        .withPayload(messagePayload)
                                        .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                                        .build()
                        );
            }
        }
    }


}